// subpage/pages/sitelist/sitelist.js
const app = getApp();
var util = app.globalData.utils
import service from '../../../utils/request'
import { formatTime, formatDate } from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currenSite: '',
    showPriceList:false,
    rateList:[],
    bannerList: [],
    groupPrice: {
      "times":[
          {
              "beginTime":"00:30",
              "endTime":"05:08:59",
              "uid":18
          },
          {
              "beginTime":"13:00",
              "endTime":"21:00:59",
              "uid":18
          }
      ],
      "list":[
          {
              "beginTime":"11:00:01",
              "discount":0,
              "discountPrice":1.1,
              "electric":1,
              "endTime":"23:59:59",
              "groupId":7,
              "id":13,
              "name":"二",
              "service":0.1,
              "total":1.1,
              "type":2
          }
      ]
    },
    imageurl:app.data.imageurl,
    option1: [{
        text: '充电方式',
        value: 0
      },
      {
        text: '快充',
        value: 1
      },
      {
        text: '慢充',
        value: 2
      },
    ],
    option2: [{
        text: '空闲数量',
        value: '0'
      },
      {
        text: '40以上',
        value: '40'
      },
      {
        text: '30以上',
        value: '30'
      },
      {
        text: '10以上',
        value: '10'
      },
    ],
    option3: [{
        text: '距离',
        value: '0'
      },
      {
        text: '小于5公里',
        value: '5'
      },
      {
        text: '小于10公里',
        value: '10'
      },
      {
        text: '小于15公里',
        value: '15'
      },
      {
        text: '小于20公里',
        value: '20'
      },
      {
        text: '小于25公里',
        value: '25'
      },
    ],
    value1: 0,
    value2: '0',
    value3: '0',
    site: {
      'unitFree': '9',
      'unitBusy': '12',
      'unitBusy': '3',
      'unitBusy': '0',
    },
    siteList: [],
    baseUrl: app.data.url,
    currentNav: 1,
    isRefresh: true,
    totalPage: 0,
    page: 1,
    isShowBottom: false,
    hasNext:false,
    type:'',
    number:'',
    ditance:'',
    name:'',
    show:false,
    rateList:[],
    rate:{},
    lng: '',
    lat: '',
    search:"",
  },
  searedit(e) {
    let that = this
    let  dataset = e.currentTarget.dataset
    let value = e.detail.value
    let searName = dataset.name
    that.setData({
      search : value
    })
    console.log('7777', this.data.search)
  },
  searbtn(){
    if(this.data.search =='') return
    this.setData({
      siteList: []
    })
    this.getSiteList()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log('我访问了地图', res)
        that.setData({
          lng: res.longitude,
          lat: res.latitude,
        })
        that.getSiteList()
      },
    })
  
    
    // this.getBanner()
  },
    //扫码
    userScen() {
      // 允许从相机和相册扫码
      wx.scanCode({
        success(res) {
          console.log(res)
          let sn = res.result;
          let index = sn.lastIndexOf("\=");
          sn = sn.substring(index + 1, sn.length);
          console.log(sn)
          wx.navigateTo({
            url: '/subpage/pages/charge/charge?sn=' + sn,
          })
        }
      })
    },
  getBanner (){
    let that = this
    wx.request({
      url: this.data.baseUrl+'/banner/list',
      method: 'GET',
      success: res => {
        console.log('banner',res)
        let data = res.data.list
        data.map(item => {
          item.image = that.data.baseUrl + item.image
          return item
        })
        that.setData({
          bannerList: data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log("下拉刷新", e)
      //如果总页数 小于等于 当前页数 提示无更多数据
      if (this.data.hasNext) {
        this.setData({
          page: this.data.page + 1,
          isRefresh:true
        })
        this.getSiteList()
      } else{
        console.log('没有更多')
        this.setData({
          isShowBottom: true
        })
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onTypeChange({ detail }) {
    console.log('选择值',this.data.value1)
    this.setData({
      type: detail,
      page: 1,
      siteList:[]
    })
    this.getSiteList()
  },
  onNumberChange({ detail }) {
    console.log('选择值',this.data.value1)
    this.setData({
      number: detail,
      page: 1,
      siteList:[]
    })
    this.getSiteList()
  },
  onDistanceChange({ detail }) {
    console.log('选择值',detail)
    this.setData({
      distance: detail,
      page: 1,
      siteList:[]
    })
    this.getSiteList()
  },

  //初始化数据
  getSiteList() {
    let that = this
    let res = service('POST', '/iot/site', {
      page: this.data.page,
      lng: this.data.lng,
      lat: this.data.lat,
      distance: 100000,
      key: this.data.search
    }).then(result => {
      if (result.data.code == 0) {
        let site = result.data.page.list
        let site1 = site.map(item => {
          if (item.distance) {
            item.distance = (item.distance / 1000).toFixed(1)
          }
          if(item.discount){
            item.discount = (item.discount).toFixed(3)+''
          }
          // if(item.pics.length >0){
          //   item.pics[0] = that.data.imageurl + item.pics[0]
          // }
          return item
        })
        console.log('附近站点', site1)
        that.setData({
          siteList: [...this.data.siteList, ...site1],
          hasNext: result.data.page.hasNext
        })
      }
    })
   },
  //导航
  navi_btn: function (e) {
    let lng = e.target.dataset.lng;
    let lat = e.target.dataset.lat;
    // var _gcj02 = util.bd09Togcj02(lng, lat);
    let address = e.target.dataset.address;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        wx.openLocation({
          latitude:Number(lat),
          longitude: Number(lng),
          name: address,
          scale: 16, //缩放比例为18
        })
      },
    })
  },
  showPrice() {
    console.log('点击了-----')
    this.setData({
      show: true
    })

  },
  onClose(){
    this.setData({
      showPriceList: false
    })
  },
        //费率列表
        handleTap(e){
          this.setData({
            showPriceList: true,
            currenSite: e.currentTarget.dataset.id
          })
          console.log('currenSite',this.data.currenSite)
        },
      //站点详情
  gosite(event){
    console.log('vvvv',event)
    wx.navigateTo({
      url: '/subpage/pages/sitedetail/sitedetail?id='+event.currentTarget.dataset.id,
    })
  },
})